package com.miracle9.game.dao;

import com.miracle9.game.entity.WaterRoom;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.stereotype.Repository;

@Repository
public class WaterRoomServiceDao extends SqlOperating<WaterRoom, Integer> {

    public WaterRoom getWaterRoomById(int id) {
        return (WaterRoom) getById(Integer.valueOf(id));
    }

    public void updateDeskNumById(int deskNum, int id) {
        String hql = "update WaterRoom set deskNum = deskNum + ? where id = ?";
        q_CreateSql(hql, new Object[]{Integer.valueOf(deskNum), Integer.valueOf(id)}).executeUpdate();
    }

    public List<WaterRoom> getWaterRooms() {
        return s_CreateSql("FROM WaterRoom Order By id ", new Object[0]).list();
    }

    public void updateOpenedDeskNumByRoomTypeAdd(int type) {
        s_CreateSql("UPDATE water_room SET opened_desk_num = opened_desk_num +1  WHERE room_type = ?", new Object[]{Integer.valueOf(type)}).executeUpdate();
    }

    public void updateOpenedDeskNumByRoomType(int type) {
        s_CreateSql("UPDATE water_room SET opened_desk_num = opened_desk_num -1  WHERE room_type = ?", new Object[]{Integer.valueOf(type)}).executeUpdate();
    }

    public void updateRemainedDeskNumByRoomTypeAdd(int type) {
        s_CreateSql("UPDATE water_room SET remained_desk_num = remained_desk_num +1  WHERE room_type = ?", new Object[]{Integer.valueOf(type)}).executeUpdate();
    }

    public void updateRemainedDeskNumByRoomType(int type) {
        s_CreateSql("UPDATE water_room SET remained_desk_num = remained_desk_num -1  WHERE room_type = ?", new Object[]{Integer.valueOf(type)}).executeUpdate();
    }

    public void getQuantity() {
        int nCount = q_CreateSqlInt("select count(*) from WaterRoom", new Object[0]);
        if (nCount == 0) {
            WaterRoom waterRoom = new WaterRoom();
            waterRoom.setId(1);
            waterRoom.setDeskNum(0);
            waterRoom.setName("新手练习厅");
            waterRoom.setType(0);
            save(waterRoom);

            waterRoom = new WaterRoom();
            waterRoom.setId(2);
            waterRoom.setName("欢乐竞技厅");
            waterRoom.setType(1);
            waterRoom.setDeskNum(0);
            save(waterRoom);
        }
    }

}
